package com.blvtphan.tipocketclubs.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.blvtphan.tipocketclubs.model.MyAccountModel;
import com.blvtphan.tipocketclubs.system.UserSetting;

public class MyAccountTableHelper {
	private DatabaseHelper mDbHelper;

	public MyAccountTableHelper(DatabaseHelper dbHelper) {
		mDbHelper = dbHelper;
	}

	/**
	 * Get my account info
	 * @return
	 */
	public MyAccountModel getMyAccountInfomation() {
		synchronized (this) {
			SQLiteDatabase db = null;

			Cursor c = null;
			MyAccountModel model = null;

			try {
				db = mDbHelper.getReadableDatabase();

				SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
				qb.setTables(DatabaseDefinition.MyAccount.TABLE);
				c = qb.query(
						db, // Database
						new String[] { DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_ID,
								DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_FIRST_NAME,
								DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_LAST_NAME,
								DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_PHONE,
								DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_EMAIL,
								DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_IMAGE,
								DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_USER_NAME,
								DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_PASSWORD,
								DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_TEAM_ID
								},
						DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_ID + "=?",
						new String[] { String.valueOf(UserSetting.getInstance().getUserId()) }, // argument[]
						null, // groupBy
						null, // having
						null // order
				);

				if (c.moveToFirst()) {
						model  = new MyAccountModel();
						model.setId(c.getLong(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_ID)));
						model.setFirstName(c.getString(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_FIRST_NAME)));
						model.setLastName(c.getString(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_LAST_NAME)));
						model.setPhone(c.getString(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_PHONE)));

						model.setEmail(c.getString(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_EMAIL)));
						model.setImage(c.getString(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_IMAGE)));
						model.setUserName(c.getString(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_USER_NAME)));
						model.setPassword(c.getString(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_PASSWORD)));
						model.setTeamId(c.getInt(c
								.getColumnIndex(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_TEAM_ID)));
				}
			} finally {
//				if (db != null) {
//					//db.close();
//				}
				
				if (c != null) {
					c.close();
				}
			}

			return model;
		}
	}

	/**
	 * Update my account info
	 * @param model
	 * @return
	 */
	public boolean updateMyAccountInformation(MyAccountModel model) {
		SQLiteDatabase db = null;
		boolean updateSuccess = false;
		try {
			db = mDbHelper.getWritableDatabase();

			try {
				ContentValues values = new ContentValues();
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_ID, model.getId());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_FIRST_NAME, model.getFirstName());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_LAST_NAME, model.getLastName());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_PHONE, model.getPhone());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_EMAIL, model.getEmail());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_IMAGE, model.getImage());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_USER_NAME, model.getUserName());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_PASSWORD, model.getPassword());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_TEAM_ID, model.getTeamId());
		
				db.update(
						DatabaseDefinition.MyAccount.TABLE, values,
						DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_ID + "=?", // �?�件(WHERE)～=?
						new String[] {
								String.valueOf(model.getId())});
				
				updateSuccess = true;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} finally {
//			if (db != null) {
//				//db.close();
//			}
		}
		
		return updateSuccess;
	}

	/**
	 * Add my account info
	 * @param model
	 * @return
	 */
	public boolean addMyAccountInformation(MyAccountModel model) {
		synchronized (this) {			
			deleteMyAccountInformation(true);
			SQLiteDatabase db = null;
			boolean isAddSuccess = false;
			try {
				db = mDbHelper.getWritableDatabase();
				ContentValues values = new ContentValues();
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_ID, model.getId());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_FIRST_NAME, model.getFirstName());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_LAST_NAME, model.getLastName());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_PHONE, model.getPhone());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_EMAIL, model.getEmail());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_IMAGE, model.getImage());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_USER_NAME, model.getUserName());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_PASSWORD, model.getPassword());
				values.put(DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_TEAM_ID, model.getTeamId());
				
				db.insertOrThrow(DatabaseDefinition.MyAccount.TABLE, null, values);
				
				isAddSuccess = true;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
//				if (db != null) {
//					//db.close();
//				}
			}
			
			return isAddSuccess;
		}
	}

	/**
	 * Delete my account info
	 * @param isInTransaction
	 * @return
	 */
	public int deleteMyAccountInformation(boolean isInTransaction) {
		int rs = -1;

		synchronized (this) {
			SQLiteDatabase db = null;
			Cursor c = null;

			try {
				db = mDbHelper.getWritableDatabase();
				try {
					rs = db.delete(DatabaseDefinition.MyAccount.TABLE,
							DatabaseDefinition.MyAccount.COLUMN_MY_ACCOUNT_ID + "= ?",
							new String[] { String.valueOf(UserSetting
									.getInstance().getUserId()) });
				} catch (SQLException e) {
					e.printStackTrace();
				}
			} finally {
//				if (db != null && !isInTransaction) {
//					//db.close();
//				}
				
				if (c != null) {
					c.close();
				}
			}
		}
		
		return rs;
	}
}
